// pages/student/mine/setUserInfo/setUserInfo.js
import {
    ajaxPost,
    showToast
}
from '../../utils/util'
let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        sexData: [
            '男',
            '女',
        ],
        educationLevelData: [],
        educationData: [],
        educationArr: [],
        educationIndex: [0, 0],
        userSource: {},
        educationSource: {},

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserInfo()
    },

    /**
     * 个人信息获取
     */
    getUserInfo() {
        let data = {
            token: wx.getStorageSync("token"),
        }
        ajaxPost("/authuser/myInfo", data, (res) => {
            if (res.code == 0) {
                let newOneArr = []
                for (let i in res.data.major_list) {
                    newOneArr.push(res.data.major_list[i].name)
                }
                let newTwoArr = []
                let arrTwo = res.data.major_list[0].data
                for (let i in arrTwo) {
                    newTwoArr.push(arrTwo[i].name)
                }
                let Arr = [
                    newOneArr,
                    newTwoArr
                ]
                this.setData({
                    userSource: res.data.user_info,
                    educationData: Arr,
                    educationArr: res.data.major_list,
                    educationLevelData: res.data.education_list,
                    educationSource: res.data.user_info.education_exp_list[0],
                })
            } else {
                showToast('text', res.msg);
            }
        });
    },
    /**
     * 改变头像
     */
    changeAvatar() {
        let _this = this
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                let file = res.tempFilePaths[0]
                wx.showLoading({
                    title: '图片上传中',
                })
                wx.uploadFile({
                    url: app.globalData.ajaxUrl + '/upload/index',
                    filePath: file,
                    name: "file",
                    formData: {
                        token: wx.getStorageSync('token')
                    },
                    success: function (e) {
                        let fileData = JSON.parse(e.data)
                        let newFile = fileData.data
                        let data = {
                            avatar: newFile,
                        }
                        _this.pustData(data)
                    },
                    fail: function (e) {
                        showToast('error', '上传失败，请重试')
                    },
                    complete: function () {}
                });
            },
        })
    },
    /**
     * 改变头像
     */
    changeResume() {
        let _this = this
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                let file = res.tempFilePaths[0]
                wx.showLoading({
                    title: '图片上传中',
                })
                wx.uploadFile({
                    url: app.globalData.ajaxUrl + '/upload/index',
                    filePath: file,
                    name: "file",
                    formData: {
                        token: wx.getStorageSync('token')
                    },
                    success: function (e) {
                        let fileData = JSON.parse(e.data)
                        let newFile = fileData.data
                        let data = {
                            image: newFile,
                        }
                        _this.pustResumeData(data)
                    },
                    fail: function (e) {
                        showToast('error', '上传失败，请重试')
                    },
                    complete: function () {}
                });
            },


        })
    },
    /**
     *  修改简历
     * @param {*} data  修改参数
     */
    pustResumeData(data) {
        data.token = wx.getStorageSync('token');
        let _this = this
        ajaxPost("/authuser/setResume", data, (res) => {
            if (res.code == 0) {
                this.getUserInfo()
                wx.hideLoading();
                showToast('success', '修改成功');
            } else {
                showToast('text', res.msg);
            }
        });

    },
    /**
     * 改变事件
     */
    changeData(e) {
        let data = e.currentTarget.dataset
        let type = data.type
        let ajaxData = {}
        switch (type) {
            case 'sex':
                /**
                 * 性别选择
                 */
                let sexIndex = e.detail.value
                let sexVal = Number(sexIndex) + 1
                ajaxData.sex = sexVal
                this.pustData(ajaxData)
                break;
            case 'birthday':
                /**
                 * 生日选择
                 */
                ajaxData.birthday = e.detail.value
                this.pustData(ajaxData)
                break;

        }
    },
    /**
     *  修改个人信息
     * @param {*} data  修改参数
     */
    pustData(data) {
        data.token = wx.getStorageSync('token');
        let _this = this
        ajaxPost("/authuser/updateUserInfo", data, (res) => {
            if (res.code == 0) {
                this.getUserInfo()
                wx.hideLoading();
                showToast('success', '修改成功');
            } else {
                showToast('text', res.msg);
            }
        });

    },

    /**
     * 学历改变
     */
    changeEducation(e) {
        let data = e.currentTarget.dataset
        let type = data.type
        let ajaxData = {}
        switch (type) {
            case 'level':
                /**
                 * 学历选择
                 */
                let levelIndex = e.detail.value
                let levelVal = this.data.educationLevelData[levelIndex]
                ajaxData.level = levelVal
                this.pustEducationData(ajaxData)
                break;
            case 'major_type':
                /**
                 * 专业类别
                 */
                let {
                    educationData,
                    educationIndex
                } = this.data
                console.log(educationData);
                console.log(educationIndex);
                let majorType = educationData[0][educationIndex[0]] + '-' + educationData[1][educationIndex[1]]
                ajaxData.major_type = majorType
                this.pustEducationData(ajaxData)
                break;

        }
    },
    bindMultiPickerColumnChange(e) {
        let educationIndex = this.data.educationIndex
        if (e.detail.column == 0 && educationIndex[0] !== e.detail.value) {
            let educationArr = this.data.educationArr
            let newTwoArr = []
            let arrTwo = educationArr[e.detail.value].data
            for (let i in arrTwo) {
                newTwoArr.push(arrTwo[i].name)
            }
            let Arr = [
                this.data.educationData[0],
                newTwoArr
            ]
            let newEducationIndex = [e.detail.value, 0]
            this.setData({
                educationData: Arr,
                educationIndex: newEducationIndex,
            })
        }
        if (e.detail.column == 1) {
            educationIndex[1] = e.detail.value
            this.setData({
                educationIndex: educationIndex,
            })
        }
    },
    /**
     * 提交学历信息
     */
    pustEducationData(data) {
        data.token = wx.getStorageSync('token');
        data.id = this.data.educationSource.id;
        data.number = 1;
        ajaxPost("/authuser/updateEducationExp", data, (res) => {
            if (res.code == 0) {
                this.getUserInfo()
                wx.hideLoading();
                showToast('success', '修改成功');
            } else {
                showToast('text', res.msg);
            }
        });
    },
})